const seneca = require('./shared-seneca');
const defaultConfig = require('../default-config.json');
const genPins = require('../gen-pins');

async function setup(options = { dontEnroll: false, permissions: defaultConfig.permissions }) {
    let { permissions } = options;
    if(!permissions) {
        options.permissions = defaultConfig.permissions;
        permissions = defaultConfig.permissions;
    }
}

function act(...args) {
    assertInvoke(...args);
    seneca.act(...args);
    return this;
}

async function actAsync(...args) {
    assertInvoke(...args);
    return new Promise((resolve, reject) => {
        seneca.act(...args, (error, result) => {
            if (error) {
                reject(error);
                return;
            }
            resolve(result);
        });
    });
}

function assertInvoke() {
    if (!seneca) {
        throw new Error('should invoke setup before got seneca invoker!');
    }
}

module.exports = {
    setup,
    get seneca() {
        assertInvoke();
        return seneca;
    },
    act,
    actAsync
};